// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-11

package responses

import (
	"gitee.com/go-libs/db-xorm/db"
	"server/app/http/models"
	"server/app/kind"
	"time"
)

// OrderResponse
// 订单信息.
type OrderResponse struct {
	Id              int64
	ArcadeId        kind.ArcadeId
	OrderNo         kind.OrderNo
	OrderKind       kind.OrderKind
	OrderKindText   string
	UserId          kind.UserId
	State           kind.OrderState
	StateText       string
	TotalAmount     kind.Balance
	PayAmount       kind.Balance
	RefundState     kind.RefundState
	RefundStateText string
	ClosedBy        kind.ClosedBy
	ClosedByText    string
	Images          []string
	CreatedAt       db.Datetime
	UpdatedAt       db.Datetime

	// 状态描述.
	CanClose   bool
	CanPay     bool
	CanPayTime string
	CanReturn  bool
	IsClosed   bool
	IsNotPaid  bool
	IsPaid     bool

	// 关系数据.
	Arcade   *ArcadeResponse         `json:",omitempty" label:"钓场"`
	User     *UserResponse           `json:",omitempty" label:"用户"`
	Items    []*OrderItemResponse    `json:",omitempty" label:"订单条目"`
	PayKinds []*OrderPaiKindResponse `json:",omitempty" label:"支付类型"`
}

// AddItem
// 添加订单条目.
func (o *OrderResponse) AddItem(m *models.OrderItem) *OrderResponse {
	if m != nil {
		if o.Items == nil {
			o.Items = make([]*OrderItemResponse, 0)
		}
		o.Items = append(o.Items, (&OrderItemResponse{}).With(m))
	}
	return o
}

// AddPayKind
// 添加支付类型.
func (o *OrderResponse) AddPayKind(m *models.OrderPayKind) *OrderResponse {
	if m != nil {
		if o.PayKinds == nil {
			o.PayKinds = make([]*OrderPaiKindResponse, 0)
		}
		o.PayKinds = append(o.PayKinds, (&OrderPaiKindResponse{}).With(m))
	}
	return o
}

// With
// 数据绑定.
func (o *OrderResponse) With(m *models.Order) *OrderResponse {
	o.Id = m.Id
	o.ArcadeId = m.ArcadeId
	o.UserId = m.UserId
	o.OrderNo = m.OrderNo
	o.OrderKind = m.OrderKind
	o.OrderKindText = m.OrderKind.Text()
	o.State = m.State
	o.StateText = m.State.Text()
	o.RefundState = m.RefundState
	o.RefundStateText = m.RefundState.Text()
	o.TotalAmount = m.TotalAmount
	o.PayAmount = m.PayAmount
	o.ClosedBy = m.ClosedBy
	o.ClosedByText = m.ClosedBy.Text()
	o.Images = m.Images
	o.CreatedAt = m.CreatedAt
	o.UpdatedAt = m.UpdatedAt

	// 状态描述.
	o.CanClose = m.CanClose()
	o.CanPay = m.CanPay()
	o.CanPayTime = m.CanPayTime().Format(time.TimeOnly)
	o.CanReturn = m.CanRefund()
	o.IsClosed = m.State.IsClosed()
	o.IsNotPaid = m.State.IsNot()
	o.IsPaid = m.State.IsSucceed()
	return o
}

// WithArcade
// 绑定钓场.
func (o *OrderResponse) WithArcade(m *models.Arcades) *OrderResponse {
	if m != nil {
		o.Arcade = (&ArcadeResponse{}).With(m)
	}
	return o
}

// WithUser
// 绑定用户.
func (o *OrderResponse) WithUser(m *models.Users) *OrderResponse {
	if m != nil {
		o.User = (&UserResponse{}).With(m)
	}
	return o
}
